package com.qzw.demo.service.service.impl;

import com.qzw.demo.service.dao.ShopDao;
import com.qzw.demo.service.entity.DmShop;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzw.demo.api.query.ShopQuery;
import java.lang.Integer;
import java.math.BigDecimal;
import com.qzw.common.data.BaseMysqlDao;
import com.qzw.demo.api.model.Shop;
import com.qzw.common.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.qzw.demo.api.service.IShopService;
import java.util.Optional;
import java.lang.String;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop, DmShop, Integer, ShopQuery> implements IShopService {

	@Autowired
	private ShopDao dao;

	@Override protected BaseMysqlDao getDao() {
	return dao;
	}

	@Override
	public Shop entity2model(DmShop entity) {
		Shop model = null;
		if(isNotEmpty(entity)){
			model = new Shop();
			model.setId(entity.getId());
			model.setName(entity.getName());
			model.setQuantity(entity.getQuantity());
			model.setAmount(entity.getAmount());
		}
		return model;
	}

	@Override
	public DmShop model2Entity(Shop model) {
		DmShop entity = null;
		if(isNotEmpty(model)){
			entity = new DmShop();
			entity.setId(model.getId());
			entity.setName(model.getName());
			entity.setQuantity(model.getQuantity());
			entity.setAmount(model.getAmount());
		}
		return entity;
	}

	@Override
	public Shop update(Shop model) {
		if(model.isNew()){
			model = super.add(model);
		} else {
			Optional<DmShop> entityOptional = dao.findById(model.getId());DmShop entity = entityOptional.get();
			if(isNotEmpty(model.getName()))
				entity.setName(model.getName());
			if(isNotEmpty(model.getQuantity()))
				entity.setQuantity(model.getQuantity());
			if(isNotEmpty(model.getAmount()))
				entity.setAmount(model.getAmount());
			model = entity2model(dao.save(entity));
		}
		return model;
	}


}